package com.wwl.cms.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/4/21 18:05
 * @description:
 */
@Slf4j
public class DateUtils {
    private static SimpleDateFormat sdf = new SimpleDateFormat();

    public static String dateFormat1(String pattern){
        Date d = new Date();
        if (StringUtils.isEmpty(pattern)){
            pattern = "yyyy-MM-dd HH:mm:sss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(d);
    }
    public static Date parseDateMilliFormat(String strDate) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        return parseDateFormat(strDate, pattern);
    }
    /**
     * 根据时间字符串和对应的格式输出时间(Date)类型,如果时间字符串和格式不匹配则出现异常
     *
     * @param strDate 时间字符串
     * @param pattern 时间字符串对应的格式pattern
     * @return java.util.Date
     */
    public static Date parseDateFormat(String strDate, String pattern) {
        if (strDate == null || strDate.trim().length() == 0) {
            return null;
        }
        synchronized (sdf) {
            StringBuffer datebuf = new StringBuffer();
            datebuf.append(strDate);
            if (strDate.length() == 10) {
                datebuf.deleteCharAt(7);
                datebuf.deleteCharAt(4);
            }
            Date date = null;
            sdf.applyPattern(pattern);
            try {
                date = sdf.parse(datebuf.toString());
            } catch (Exception e) {
                log.error("DataUntil", e);
            }
            return date;
        }
    }


}
